package com.example.tianxi.yuexiuhhpatient.fragment;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tianxi.yuexiuhhpatient.PersonalInfoActivity;
import com.example.tianxi.yuexiuhhpatient.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SettingFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SettingFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SettingFragment extends Fragment {


    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

//    private OnFragmentInteractionListener mListener;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment SettingFragment.
     */


    public static SettingFragment newInstance() {
        SettingFragment fragment = new SettingFragment();

        return fragment;
    }

    // TODO: Rename and change types and number of parameters
    public static SettingFragment newInstance(String param1, String param2) {
        SettingFragment fragment = new SettingFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public SettingFragment() {
        // Required empty public constructor
    }

    public static final String TAG = "SettingFragment";

    private static SettingFragment settingFragment;

    View view;
    LayoutInflater inflater;
    private RelativeLayout personInfo;

    public static SettingFragment getInstance() {
        if (settingFragment == null) {
            settingFragment = new SettingFragment();
        }
        return settingFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_setting, container, false);
        initView();
        initData();
        return view;
    }

    public void initView(){
        TextView title = (TextView)view.findViewById(R.id.title);
        title.setText("我");
    }
    public void initData(){
        ClickListener clickListener = new ClickListener();

        personInfo = (RelativeLayout)view.findViewById(R.id.personal_info);
        personInfo.setOnClickListener(clickListener);


    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
//        if (mListener != null) {
//            mListener.onFragmentInteraction(uri);
//        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
//        try {
//            mListener = (OnFragmentInteractionListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
//    public interface OnFragmentInteractionListener {
//        // TODO: Update argument type and name
//        public void onFragmentInteraction(Uri uri);
//    }


    class ClickListener implements OnClickListener {

        Intent intent;
        @Override
        public void onClick(View clickedView) {
            switch (clickedView.getId()) {
                case R.id.personal_info:
                    intent = new Intent(getActivity(), PersonalInfoActivity.class);
                    intent.putExtra("name","王二");
                    intent.putExtra("sex", "男");
                    intent.putExtra("birth", "45岁");
                    intent.putExtra("tel", "18792123652");
                    intent.putExtra("introduction", "肠炎，外加轻微的胃炎，小肠气");
                    getActivity().startActivityForResult(intent, 205);
                    break;

                case R.id.charge:
//                    intent = new Intent(getActivity(), PersonalChargeActivity.class);
//                    startActivity(intent);
                    break;

                case R.id.personal_setting:
//                    intent = new Intent(getActivity(), SettingActivity.class);
//                    intent.putExtra("userId", userId);
//                    getActivity().startActivity(intent);
                    break;

                case R.id.personal_code:
//                    try {
//                        if (!userId.equals("")) {
//                            //根据字符串生成二维码图片并显示在界面上，第二个参数为图片的大小（350*350）
//                            Bitmap qrCodeBitmap = EncodingHandler.createQRCode(identifier, 300);
//                            dialog = new Dialog(getActivity(), R.style.progress_dialog);
//                            dialog.setContentView(R.layout.dialog_personal_code);
//                            dialog.setCancelable(true);
//                            dialog.getWindow().setBackgroundDrawableResource(android.R.color.background_dark);
//                            ImageView codeImageView = (ImageView)dialog.findViewById(R.id.code_image);
//                            codeImageView.setImageBitmap(qrCodeBitmap);
//                            dialog.show();
//                            dialog.setCanceledOnTouchOutside(true);
//                        }else {
//                            Toast.makeText(getActivity(), "Text can not be empty", Toast.LENGTH_SHORT).show();
//                        }
//
//                    } catch (WriterException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
                    break;
                default:
                    break;
            }

        }

    }

}
